package cn.hn.yh.service.fhyc;

import cn.hn.yh.common.DataStory;
import cn.hn.yh.dao.fhyc.st.StDdFhycDao;
import cn.hn.yh.serviceInterface.fhyc.StDdFhycServiceInf;
import cn.hn.yh.vo.Base96TVO;
import cn.hn.yh.vo.NetBase96VO;
import cn.hn.yh.vo.NetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class StDdFhycService implements StDdFhycServiceInf {

    @Autowired
    private StDdFhycDao stDdFhycDao;

    @Override
    public List<String[]> getStDdFhyc(String pdate, String netid) {
        String[] column = DataStory.DAY_LOAD_96COLUMN;
        List<String[]> list = new ArrayList<>();
        String cols = "netname," + Arrays.toString(column).substring(1).replaceAll("]", "");
        NetBase96VO data = this.getStDdFhyc(cols, pdate.replaceAll("-", ""), netid);
        String d5000Id = stDdFhycDao.getD5000NetId(netid);
        String month = pdate.replaceAll("-", "").replaceAll("/", "").substring(0, 6);
        Base96TVO realValue = this.getRealValue(cols.substring(8, cols.length()), month, d5000Id, pdate);
        String[] tmp = new String[96];
        if (data == null) {
            for (int i = 0; i < 96; i++) {
                tmp[i] = "---";
            }
            String[] s = new String[1];
            s[0] = "---";
            list.add(s);
            list.add(tmp);
        } else {
            String[] s = new String[1];
            s[0] = data.getNetname();
            list.add(s);
            list.add(data.get96Stringlist());
        }
        if (realValue == null) {
            for (int i = 0; i < 96; i++) {
                tmp[i] = "---";
            }
            list.add(tmp);
        } else {
            list.add(realValue.get96Stringlist());
        }
        if (realValue == null) {
            for (int i = 0; i < 96; i++) {
                tmp[i] = "---";
            }
            list.add(tmp);
        } else {
            list.add(realValue.get96Stringlist());
        }
        return list;
    }

    @Override
    public String[] getStDdFhyc_avg1(String pdate, String netid) {
        String[] column = DataStory.DAY_LOAD_96COLUMN;
        DecimalFormat df = new DecimalFormat("0.00");
        double sum = 0.0;
        int count = 0;
        String rate = "";
        String[] str = new String[3];
        String submittime = this.querySubmitDate(pdate.replaceAll("-", "").replaceAll("/", ""), netid);
        String cols = "netname," + Arrays.toString(column).substring(1).replaceAll("]", "");
        NetBase96VO fcValue = stDdFhycDao.getStDdFhyc(cols, pdate.replaceAll("-", ""), netid);
        String d5000Id = stDdFhycDao.getD5000NetId(netid);
        String month = pdate.replaceAll("-", "").replaceAll("/", "").substring(0, 6);
        Base96TVO hisValue = stDdFhycDao.getRealValue(cols.substring(8, cols.length()), month, d5000Id, pdate);
        String[] plan = fcValue == null ? null : fcValue.get96Stringlist();
        String[] real = hisValue == null ? null : hisValue.get96Stringlist();
        if (plan != null && real != null) {
            for (int i = 0; i < 96; i++) {
                boolean fcFlag = false;
                boolean hisFlag = false;
                double fc = 0.0;
                double his = 0.0;
                if (plan[i] != null && !plan[i].equals("")) {
                    fcFlag = true;
                    fc = Double.parseDouble(plan[i]);
                }
                if (real[i] != null && !real[i].equals("")) {
                    hisFlag = true;
                    his = Double.parseDouble(real[i]);
                }
                if (fcFlag && hisFlag) {
                    sum += Math.pow((fc - his) / his, 2);
                    count++;
                }
            }
            rate = df.format((1 - Math.sqrt(sum / count)) * 100);
        } else {
            rate = "--";
        }
        str[0] = this.getNetInfoById(netid).getNetname();
        str[1] = rate;
        str[2] = submittime;
        return str;
    }

    @Override
    public List<String[]> getStDdFhyc_avg2(String pdate) {
        String[] column = DataStory.DAY_LOAD_96COLUMN;
        DecimalFormat df = new DecimalFormat("0.00");
        List<String[]> list = new ArrayList<>();
        List<NetVO> netList = this.getNetInfo();
        for (int m = 0; m < netList.size(); m++) {
            String[] str = new String[3];
            double sum = 0.0;
            int count = 0;
            String rate = "";
            String netid = netList.get(m).getNetid();
            String netname = netList.get(m).getNetname();
            String submittime = this.querySubmitDate(pdate.replaceAll("-", "").replaceAll("/", ""), netid);
            String cols = Arrays.toString(column).substring(1).replaceAll("]", "");
            NetBase96VO fcValue = stDdFhycDao.getStDdFhyc(cols, pdate.replaceAll("-", ""), netid);
            String d5000Id = stDdFhycDao.getD5000NetId(netid);
            String month = pdate.replaceAll("-", "").replaceAll("/", "").substring(0, 6);
            Base96TVO hisValue = stDdFhycDao.getRealValue(cols, month, d5000Id, pdate);
            String[] plan = fcValue == null ? null : fcValue.get96Stringlist();
            String[] real = hisValue == null ? null : hisValue.get96Stringlist();
            if (plan != null && real != null) {
                for (int i = 0; i < 96; i++) {
                    boolean fcFlag = false;
                    boolean hisFlag = false;
                    double fc = 0.0;
                    double his = 0.0;
                    if (plan[i] != null && !plan[i].equals("")) {
                        fcFlag = true;
                        fc = Double.parseDouble(plan[i]);
                    }
                    if (real[i] != null && !real[i].equals("")) {
                        hisFlag = true;
                        his = Double.parseDouble(real[i]);
                    }
                    if (fcFlag && hisFlag) {
                        sum += Math.pow((fc - his) / his, 2);
                        count++;
                    }
                }
                rate = df.format((1 - Math.sqrt(sum / count)) * 100);
            } else {
                rate = "--";
            }
            str[0] = netname;
            str[1] = rate;
            str[2] = submittime;
            list.add(str);
        }
        return list;
    }

    public NetBase96VO getStDdFhyc(String cols, String pdate, String netid) {
        return stDdFhycDao.getStDdFhyc(cols, pdate, netid);
    }

    public Base96TVO getRealValue(String cols, String month, String d5000id, String pdate) {
        return stDdFhycDao.getRealValue(cols, month, d5000id, pdate);
    }

    public NetVO getNetInfoById(String netid) {
        return stDdFhycDao.getNetInfoById(netid);
    }

    public List<NetVO> getNetInfo() {
        return stDdFhycDao.getNetInfo();
    }

    public String querySubmitDate(String pdate, String netid) {
        return stDdFhycDao.querySubmitDate(pdate, netid);
    }

    @Override
    public List<String[]> getStDdFhyc_vue(String pdate, String netid) {
        String[] column = DataStory.DAY_LOAD_96COLUMN;
        List<String[]> list = new ArrayList<>();
        String cols = "netname," + Arrays.toString(column).substring(1).replaceAll("]", "");
        NetBase96VO data = this.getStDdFhyc(cols, pdate.replaceAll("-", ""), netid);
        String d5000Id = stDdFhycDao.getD5000NetId(netid);
        String month = pdate.replaceAll("-", "").replaceAll("/", "").substring(0, 6);
        Base96TVO realValue = this.getRealValue(cols.substring(8, cols.length()), month, d5000Id, pdate);
        String[] tmp = new String[96];
        /*预测值*/
        if (data == null) {
            for (int i = 0; i < 96; i++) {
                tmp[i] = null;
            }
            String[] s = new String[1];
            s[0] = "---";
            list.add(s);
            list.add(tmp);
        } else {
            String[] s = new String[1];
            s[0] = data.getNetname();
            list.add(s);
            list.add(data.get96Stringlist());
        }
        /*实际值*/
        if (realValue == null) {
            for (int i = 0; i < 96; i++) {
                tmp[i] = null;
            }
            list.add(tmp);
        } else {
            list.add(realValue.get96Stringlist());
        }
        System.out.println(list.get(2)[33]);
//        if (realValue == null) {
//            for (int i = 0; i < 96; i++) {
//                tmp[i] = "---";
//            }
//            list.add(tmp);
//        } else {
//            list.add(realValue.get96Stringlist());
//        }
        return list;
    }
}
